package com.quan.weixin.api.commons;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.binarywang.wxpay.bean.request.WxPaySendRedpackRequest;
import com.github.binarywang.wxpay.bean.result.WxPaySendRedpackResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.quan.common.bean.R;
import com.quan.common.core.MyBaseController;
import com.quan.common.enums.DateStyle;
import com.quan.common.utils.DateUtils;
import com.quan.common.utils.IpHelper;
import com.quan.common.utils.RandomUitl;

import lombok.AllArgsConstructor;

@AllArgsConstructor
@RestController
@RequestMapping("/api/wx/common/redpack")
public class WxRedpackController extends MyBaseController {

	@Autowired
	private WxPayService wxPayService;
	
	@PostMapping(value = "/sendRedpack")
	public Object sendRedpack(@RequestBody WxPaySendRedpackRequest redpackRequest) {
		try {
			
			//订单号：mch_id+yyyymmdd+10
			String mchBillNo = this.wxPayService.getConfig().getMchId() + DateUtils.format(new Date(), DateStyle.YYYYMMDD)+RandomUitl.generateInteger(10) ;
			
			redpackRequest.setMchBillNo(mchBillNo);
			redpackRequest.setClientIp(IpHelper.getIpAddr(request));
			WxPaySendRedpackResult result = this.wxPayService.getRedpackService().sendRedpack(redpackRequest);
			
			logger.info("==>发送红包：{}", result.getReturnMsg());
		} catch (WxPayException e) {
			e.printStackTrace();
		}
		
		return R.ok();
	}
	
}
